/*
//  Copyright © 2014 Allen County Public Library.

  //        ACPL Mobile is free software: This program is distributed and you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.  ACPL Mobile is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, including without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ACPL Mobile. If not, see http://www.gnu.org/licenses/.
 * Author: James Harpe
 * 
 * 
 * 
 * Description: This activity displays a list of search results. When the user scrolls to the bottom of the list,
 * 				additional results are loaded.
 */

package us.in.lib.acpl.Mobile_ACPL;


import com.google.analytics.tracking.android.EasyTracker;

import us.in.lib.acpl.Mobile_ACPL.adapters.SearchResultListAdapter;
import us.in.lib.acpl.Mobile_ACPL.context.Mobile_ACPL;
import us.in.lib.acpl.Mobile_ACPL.parents.BaseListActivity;
import us.in.lib.acpl.Mobile_ACPL.ws.MainSearchHandler;
import us.in.lib.acpl.Mobile_ACPL.ws.SearchCatalogHandler;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;

public class SearchResultsActivity extends BaseListActivity implements
		OnScrollListener {

	private SearchCatalogHandler handler;
	private int page = 1;
	private boolean readyForMore = true;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent intent = getIntent();

		displaySearchResults();

	}
	  @Override
	  public void onStart() {
	    super.onStart();
	    EasyTracker.getInstance().activityStart(this); // Add this method.
	  }

	  @Override
	  public void onStop() {
	    super.onStop();
	    EasyTracker.getInstance().activityStop(this); // Add this method.
	  }
	public void displaySearchResults() {
		Mobile_ACPL app = (Mobile_ACPL) this.getApplication();

		if (getIntent().getStringExtra("source").equalsIgnoreCase("MoreAuthor")) {
			handler = app.getAuthorSearchHandler();
		} else if(getIntent().getStringExtra("source").equalsIgnoreCase("SearchActivity")){
			handler = app.getSearchHandler();
		}else{
			handler = app.getBestSellerSearchHandler();
		}

		if(handler==null){
			Toast toast = Toast.makeText(this, "An error has occurred. Please try your search again.", Toast.LENGTH_LONG);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			return;
		}
		this.getActionBar().setTitle(
				"  Search Results");
		SearchResultListAdapter adapter = new SearchResultListAdapter(this,
				handler.getResults());
		

		this.setContentView(R.layout.search_result);
		
		LinearLayout footerView = (LinearLayout) this.getLayoutInflater()
				.inflate(R.layout.pending, null);
		
		this.getListView().addFooterView(footerView);
		if(handler.getResults()!=null){
			setListAdapter(adapter);
		}else{
			Toast toast = Toast.makeText(this, "Scan failed.",
					Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
		}

		if (handler.getTotalResults() == 0) {
			this.findViewById(R.id.noResults).setVisibility(View.VISIBLE);
		}

		this.getListView().setOnItemClickListener(new ListListener());
		this.getListView().setOnScrollListener(this);

	}

	public void showMoreResults() {
		this.findViewById(R.id.resultsBar).setVisibility(View.GONE);
		readyForMore = true;
	}

	private class ListListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {

			Intent intent = new Intent(parent.getContext(),
					SearchResultsTitleDetailActivity.class);
			intent.putExtra("title", handler.getResults().get(position)
					.getTitle());
			intent.putExtra("author", handler.getResults().get(position)
					.getAuthor());
			intent.putExtra("isbn", handler.getResults().get(position)
					.getIsbn());
			intent.putExtra("librariesWithAvailableCopies", handler
					.getResults().get(position)
					.getLibrariesWithAvailableCopies());
			intent.putExtra("holdable", handler.getResults().get(position)
					.isHoldable());
			intent.putExtra("titleID", handler.getResults().get(position)
					.getTitleID());
			intent.putExtra("yearOfPublication",
					handler.getResults().get(position).getYearOfPublication());
			intent.putExtra("materialType", handler.getResults().get(position)
					.getMaterialType());
			intent.putExtra("callNum", handler.getResults().get(position)
					.getCallNumber());
			intent.putExtra("edition", handler.getResults().get(position)
					.getEdition());
			intent.putExtra("titleID", handler.getResults().get(position)
					.getTitleID());
			intent.putExtra("url", handler.getResults().get(position).getUrl());
			startActivity(intent);
		}

	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {

		if (view.getAdapter() != null
				&& ((firstVisibleItem + visibleItemCount) >= totalItemCount)
				&& readyForMore) {

			if (handler.hasMoreResults()) {
				

				EasyTracker.getTracker().sendEvent("Search", "Get More Results", null, null);
				
				this.getListView().findViewById(R.id.resultsBar)
						.setVisibility(View.VISIBLE);
				readyForMore = false;
				
				handler.getMoreResults(this);
			}
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView arg0, int arg1) {

	}
	


}
